package com.wskeee.ec.components
{
	import com.wskeee.ec.ens;
	import com.wskeee.ec.components.skin.AlertSkin;
	import com.wskeee.ec.core.SkinableComponent;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class Alert extends SkinableComponent
	{
		use namespace ens;
		public function Alert(text:String="",
							  title:String="",
							  flag:uint=1,
							  explicitParent:DisplayObjectContainer = null,
							  modal:Boolean = true,
							  closeHandler:Function = null)
		{
			border = true;
			this.text = text;
			this.title = title;
			this.flag = flag;
			this.explicitParent = explicitParent;
			this.closeHandler = closeHandler;
			this.modal = modal;
			super();
		}
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  Class Static Variables
		//-----------------------------------------------------------------
		public static const ENTER:uint = 1;
		public static const CANCEL:uint = 2;
		/**
		 * 所有 Alert 实例 
		 */
		private static var instances:Vector.<Alert> = new Vector.<Alert>();
		
		/**
		 * 按钮显示文字 
		 */		
		public static var enterLabel:String = "Yes";
		public static var cancelLabel:String = "No";
		
		private static var _stage:Stage;
		//-----------------------------------------------------------------
		//  Instance Variables
		//-----------------------------------------------------------------
		
		/**
		 * 标题 
		 */
		ens var title:String;
		/**
		 * 信息 
		 */
		ens var text:String;
		/**
		 * 控制标识，1确认，2取消，3确认+取消（1|2）
		 */
		ens var flag:int;
		
		/**
		 * 隔离,设置 true，alert 弹出时，将冻结 alert 层下的界面 
		 */
		ens var modal:Boolean = true;
		
		/**
		 * 标准 alert 容器，当设置该值，计算高宽时将以explicitParent为参照物居中
		 */
		ens var explicitParent:DisplayObjectContainer;

		/**
		 * 关闭回调 
		 */
		private var closeHandler:Function;
		
		/**
		 * 用于包装 alert ，可以冻结舞台 
		 */
		private var container:Sprite;
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  static stage
		//-----------------------------------------------------------------
		/**
		 * 设置所有 alert stage 
		 * @param value
		 * 
		 */
		public static function set stage(value:Stage):void
		{
			_stage = value;
		}
		
		public static function get stage():Stage
		{
			return _stage;
		}
		/**
		 * 信息显示文本 
		 * @return 
		 * 
		 */
		private function get textFiled():TextField
		{
			return Object(skinDrawer).textFiled;
		}
		
		/**
		 * 标题显示文本 
		 * @return 
		 * 
		 */
		private function get titleFiled():TextField
		{
			return Object(skinDrawer).titleFiled;
		}
		
		/**
		 * 确定按钮 
		 * @return 
		 * 
		 */
		private function get enterButton():Button
		{
			return Object(skinDrawer).enterButton;
		}
		/**
		 * 取消按钮 
		 * @return 
		 * 
		 */
		private function get cancelButton():Button
		{
			return Object(skinDrawer).cancelButton;
		}
		
		//---------------------
		//skin
		//---------------------
		override public function get DefaultSkinDrawer():Class
		{
			return AlertSkin;
		}
		//--------------------------------------------------------------------------
		//
		//  Method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  static public Method
		//-----------------------------------------------------------------
		/**
		 * 显示弹出提示窗口 
		 * @param text				显示信息
		 * @param title				显示标题
		 * @param flag				控制按钮,1确定,2取消,1|2确定+取消
		 * @param explicitParent	参考容器
		 * @param modal				冻结，隔离
		 * @param closeHandler		关闭回调
		 * @return 
		 * 
		 */
		public static function show(text:String="",
									title:String="",
									flag:uint=1,
									explicitParent:DisplayObjectContainer = null,
									modal:Boolean = true,
									closeHandler:Function = null):Alert
		{
			var instance:Alert = new Alert(text,title,flag,	explicitParent,modal,closeHandler);
			instances.push(instance);
			return instance;
		}
		
		/**
		 * 移除所有 alert  
		 * 
		 */
		public static function removeAll():void
		{
			while(instances.length>0)
				removeAlertByIndex(0);
		}
		
		/**
		 * 删除 alert by index 
		 * @param value
		 * 
		 */
		public static function removeAlertByIndex(value:int):void
		{
			if(value!=-1)
			{
				var alert:Alert = instances[value];
				instances.splice(value,1);
				alert.destory();
			}
		}
		
		/**
		 * 删除 alert 
		 * @param alert
		 * 
		 */
		public static function removeAlert(alert:Alert):void
		{
			removeAlertByIndex(instances.indexOf(alert));
		}
		
		/**
		 * 返回活动的 alert,即 the last alert 
		 * @return 
		 * 
		 */
		public static function activeAlert():Alert
		{
			if(instances.length>0)
				return instances[instances.length-1]
			return null;
		}
		
		/**
		 * 设置 alert 为活动，调整为最高层 
		 * @param alert
		 * 
		 */
		public static function setAlertActive(alert:Alert):void
		{
			var index:int = instances.indexOf(alert)
			if(index!=-1 && index!=instances.length-1)
			{
				instances.splice(index,1);
				instances.push(alert);
				alert.parent.addChild(alert);
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Instance Method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  children
		//-----------------------------------------------------------------
		override protected function initChildren():void
		{
			super.initChildren();
			initContainer();
			if(modal)
				freeze();
		}
		//-----------------------------------------------------------------
		//  container
		//-----------------------------------------------------------------
		private function initContainer():void
		{
			container = new Sprite();
			container.addChild(this);
			Alert.stage.addChild(container);
		}
		//---------------------
		//initSkin
		//---------------------
		override protected function initSkin():void
		{
			super.initSkin();
			initEnterButton();
			initCancelButton();
		}
		//-----------------------------------------------------------------
		//  initEvent
		//-----------------------------------------------------------------
		override protected function initEvent():void
		{
			super.initEvent();
			addEventListener(Event.RESIZE,onResizeHandler);
			Alert.stage.addEventListener(Event.RESIZE,onStateResizeHandler);
		}
		
		private function onStateResizeHandler(event:Event):void
		{
			if(modal)
				freeze();
			center();
		}
		
		private function onResizeHandler(event:Event):void
		{
			center();
		}
		
		//---------------------
		//EnterButton
		//---------------------
		private function initEnterButton():void
		{
			enterButton.addEventListener(MouseEvent.CLICK,onEnterButtonClickHandler);
		}
		
		private function onEnterButtonClickHandler(event:MouseEvent):void
		{
			close(ENTER);
		}
		
		//---------------------
		//CancelButton
		//---------------------
		private function initCancelButton():void
		{
			cancelButton.addEventListener(MouseEvent.CLICK,onCancelButtonClickHandler);
		}
		
		private function onCancelButtonClickHandler(event:MouseEvent):void
		{
			close(CANCEL);
		}
		
		//---------------------
		//close
		//---------------------
		private function close(flag:uint):void
		{
			if(closeHandler!=null)
				closeHandler({detail:flag});
			remove();
		}
		
		//---------------------
		//freeze
		//---------------------
		
		/**
		 * 冻结 舞台 使用用户无法操作,当 madol 为 true 时调用  
		 * @param bo
		 * 
		 */
		private function freeze(bo:Boolean=true):void
		{
			var g:Graphics = container.graphics;
			g.clear();
			if(bo)
			{
				g.beginFill(getStyle("themeSelectedColor") as uint,0.3);
				g.drawRect(0,0,Alert.stage.stageWidth,Alert.stage.stageHeight);
			}
		}
		//--------------------------------------------------------------------------
		//
		//  public Method
		//
		//--------------------------------------------------------------------------
		public function center():void
		{
			var p:DisplayObjectContainer = explicitParent ? explicitParent : Alert.stage;
			var maxWidth:Number = explicitParent ? explicitParent.width : Alert.stage.stageWidth;
			var maxHeight:Number = explicitParent ? explicitParent.height : Alert.stage.stageHeight;
			
			x = (maxWidth - width)/2;
			y = (maxHeight - height)/2;
		}
		/**
		 * 清除 alert 
		 * 
		 */
		public function remove():void
		{
			Alert.removeAlert(this);
		}
		
		//---------------------
		//destory
		//---------------------
		override public function destory():void
		{
			removeEventListener(Event.RESIZE,onResizeHandler);
			enterButton.removeEventListener(MouseEvent.CLICK,onEnterButtonClickHandler);
			cancelButton.removeEventListener(MouseEvent.CLICK,onCancelButtonClickHandler);
			
			Alert.stage.removeEventListener(Event.RESIZE,onStateResizeHandler);
			Alert.stage.removeChild(container);
			super.destory();
		}
	}
}